// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()

//  查询函数
const seachItem = async (res) => {
  let dbList = await db.collection('data_list').where({
    _id: db.command.in(res)
  }).get()
  return dbList
}

//  增加函数
const addItem = async (dbName, data) => {
  try {
    return await db.collection(dbName).add({
      // data 字段表示需新增的 JSON 数据
      data: data
    })
  } catch (e) {
    console.error(e)
  }
}

//  删除函数
const delItem = async (res) => {
  let dbList = await db.collection('data_list').where({
    _id: db.command.in(res)
  }).get()
  return dbList
}
//  更新函数
const updateItem = async (res) => {
  let dbList = await db.collection('data_list').where({
    _id: db.command.in(res)
  }).get()
  return dbList
}
//将即将添加的数据按规则生成
const getAddList = (

) => {
  let data = {
    _id: "",
    update_time: new Date().toString(),
    date: []
  };
  let startTime = item.start_time.time;
  let endTime = item.end_time.time;
  let dayNum = getDayNum(startTime, endTime);
  let timeList = [];
  if (dayNum == 0) {
    dayNum == 1
  } else {
    ++dayNum
  }
  timeList.push(getDates(dayNum, startTime))
  for (let i = 0; i < timeList.length; i++) {

  }
  if (event.end_time.time == event.start_time.time) {
    data._id = startArr[0];
    data.date[startArr[1]] = {
      day: []
    };
    data.date[startArr[1]][startArr[2]] = {
      list: [{
        id: Date.now().toString(),
        title: event.title,
        desc: event.desc,
        time: event.time
      }]
    }
    list.push(data);
    return list;
    console.log(JSON.stringify(list));
  } else {
    if (startArr[0] != endArr[0]) {

    } else {

    }
  }
}

//获取时间差值
const getDayNum = (start, end) => {
  console.log(88888888888888888888888888888);
  start = Date.parse(new Date(start)) / 1000;
  end = Date.parse(new Date(end)) / 1000;
  let num = parseInt((start - end) / 86400);
  console.log(num);
  return num;
}
//计算重复事件的时间
const getDates = (days, todate) => {
  var dateArry = [];
  for (var i = 0; i < days; i++) {
    var dateObj = dateLater(todate, i);
    dateArry.push(dateObj)
  }
  return dateArry;
}
const dateLater = (dates, later) => {
  let dateObj = {};
  let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  let date = new Date(dates);
  date.setDate(date.getDate() + later);
  let day = date.getDay();
  let yearDate = date.getFullYear();
  let month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
  let dayFormate = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  dateObj.time = yearDate + '-' + month + '-' + dayFormate;
  dateObj.week = show_day[day];
  return dateObj;
}

// 云函数入口函数
exports.main = async(event, context) => {
  console.log(event);
  console.log();
  console.log(event.start_time.time);
  // const wxContext = cloud.getWXContext();
  
  let endArr = event.end_time.split("-");
  let startArr = event.start_time.split("-");
  var list = []; //添加的年份值

  var paramsList = []; //最终查询出来的值
  var idNumList = []; //条件搜索的变量
  var updateNum = []; //需要向数据库里面新增的数据
  var dbListMap = new Map();


  console.log(1111111111111111111111111111111);
  console.log(getDayNum(event.start_time, event.end_time));
  getDates(getDayNum(event.start_time, event.end_time) + 1, event.start_time);
  console.log(getDates(getDayNum(event.start_time, event.end_time) + 1, event.start_time));

  if (endArr[0] == startArr[0]) {
    idNumList = [startArr[0]];
  } else {
    idNumList = [startArr[0], endArr[0]];
  }
  let dbList = await seachItem(idNumList);
  console.log(dbList);

  for (let i = 0; i < dbList.length; i++) {
    dbListMap.set(dbList[i]._id, dbList[i]);
  }

  for (let i = 0; i < idNumList.length; i++) {
    if (!dbListMap.get(idNumList[i])) {
      updateNum.push(idNumList[i])
    }
  }

  console.log(JSON.stringify(idNumList));

}

